package com.ios.palette.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ios.palette.domain.User;
import com.ios.palette.respVO.RespStatus;
import com.ios.palette.respVO.Result;
import com.ios.palette.domain.Status;
import com.ios.palette.service.IUserService;
import com.ios.palette.service.IStatusService;
import com.ios.palette.utils.Upload;

/**
 * @author 志刚
 *
 */
@Controller
@RequestMapping(value = "/status/")
public class StatusController {

	@Autowired
	private IStatusService statusService;
	@Autowired
	private IUserService userService;

	/**
	 * @param status
	 * @param production
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "publish")
	@ResponseBody
	public Result publish(@RequestBody Status status, @RequestParam List<MultipartFile> pics,
			HttpServletRequest request) throws Exception {
		Result result = new Result("201", "发布失败", status);
		User user = (User) request.getSession().getAttribute("user");
		status.setUser(user);
		if (pics != null) {
			Upload upload = new Upload();
			Set<String> picsPath=new HashSet<>();
			for (MultipartFile pic : pics) {
				String statusPath = upload.uploadAndGetPath(pic, request);
				picsPath.add(statusPath);
			}
			status.setPics(picsPath);
		}
		if (statusService.add(status) != null) {
			Set<Status> set = new HashSet<>();
			if (user.getstatues() != null)
				set = user.getstatues();
			set.add(status);
			user.setstatues(set);
			userService.update(user);
			result.setStatus("200");
			result.setMsg("发布成功");
			result.setData(status);
		}
		return result;
	}

	/**
	 * @param status
	 * @param production
	 * @param request
	 * @return
	 */
	/*
	 * @RequestMapping(value = "update")
	 * 
	 * @ResponseBody public Result update(@RequestBody Status
	 * status, @RequestParam MultipartFile production, HttpServletRequest
	 * request) throws Exception { Result result = new Result("201", "修改失败",
	 * status); if (production != null) { Upload upload = new Upload(); String
	 * statusPath = upload.uploadAndGetPath(production, request); File file =
	 * new File(status.getProduction()); if (file.exists()) file.delete();
	 * status.setProduction(statusPath); } statusService.update(status);
	 * result.setStatus("200"); result.setMsg("更新成功"); return null;
	 * 
	 * }
	 */
	/**
	 * @param status
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public Result delete(@RequestBody Status status, HttpServletRequest request) throws Exception {
		Result result = new Result("201", "删除失败", status);
		User user = (User) request.getSession().getAttribute("user");
		Set<Status> set = new HashSet<>();
		if (user.getstatues() != null)
			set = user.getstatues();
		if (set == null)
			return result;
		set.remove(status);
		user.setstatues(set);
		userService.update(user);
		result.setStatus("200");
		result.setMsg("删除成功");
		return result;
	}

	@RequestMapping(value = "getById")
	@ResponseBody
	public Result getById(Long id) {
		Result result = new Result("201", "查看失败", null);
		Status status = statusService.get(Status.class, id);
		if (status != null) {
			result.setStatus("200");
			result.setMsg("查找成功");
			result.setData(status);
		}
		return result;
	}

	@RequestMapping(value = "getByTag")
	@ResponseBody
	public Result getByTag(String tag) {
		Result result = new Result("201", "查看失败", null);
		Status status = statusService.get(tag);
		if (status != null) {
			result.setStatus("200");
			result.setMsg("查找成功");
			result.setData(status);
		}
		return result;
	}

//	@RequestMapping(value = "search")
//	@ResponseBody
//	public Result search(String tag) {
//		Result result = new Result("201", "查找失败", null);
//		List<Status> list = statusService.searchByTag(tag);
//		if (list != null) {
//			result.setStatus("200");
//			result.setMsg("查找成功");
//			result.setData(list);
//		}
//		return result;
//	}

	@RequestMapping(value = "sortByPraise")
	@ResponseBody
	public Result sortByPraise() {
		Result result = new Result("201", "排序失败", null);
		List<Status> list = statusService.sortDesc(Status.class, "praise");
		if (list != null) {
			result.setStatus("200");
			result.setMsg("排序成功");
			result.setData(list);
		}
		return result;
	}

	@RequestMapping(value = "sortByCollected")
	@ResponseBody
	public Result sortByCollected() {
		Result result = new Result("201", "排序失败", null);
		List<Status> list = statusService.sortDesc(Status.class, "collected");
		if (list != null) {
			result.setStatus("200");
			result.setMsg("排序成功");
			result.setData(list);
		}
		return result;
	}

	@RequestMapping(value = "getWorldStatusList")
	@ResponseBody
	public Result getWorldStatusList(int userId, int page, int num){
		Result result=new Result("200",null,"查找失败");
		List<RespStatus> rsList=statusService.getWorldStatusList(userId, page, num);
		result.setStatus("201");
		result.setData(rsList);
		result.setMsg("查找成功");
		return result;
	}
}
